package com.zhhl.redisTest;

import java.io.ByteArrayOutputStream;
import java.io.ObjectOutputStream;
import java.util.Set;

import com.jld.base.common.bean.business.RedisBean;
import com.zhhl.redis.RedisGeneratorDao;

import redis.clients.jedis.Jedis;


public class Demo_set {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		Jedis jedis = new Jedis("localhost",6379);  //����redis���� 
		
		jedis.sadd("sname", "wobby");  
        jedis.sadd("sname", "kings");  
        jedis.sadd("sname", "demon");  
        System.out.println(String.format("set num: %d", jedis.scard("sname")));  //总数
        System.out.println(String.format("all members: %s", jedis.smembers("sname")));  //所有的值
        System.out.println(String.format("is member: %B", jedis.sismember("sname", "wobby")));  //判断是否存在
        System.out.println(String.format("rand member: %s", jedis.srandmember("sname"))); //随机获取数据 
        
        jedis.srem("sname", "demon");  //删除一个对象  
        System.out.println(String.format("all members: %s", jedis.smembers("sname")));
//        jedis.del("sname");
        
        
        jedis.del("hackers");
        RedisBean rb1= new RedisBean("111", "111");
        
        jedis.zadd("hackers".getBytes(), 1900, serialize(rb1));  
        jedis.zadd("hackers", 1940, "Alan Kay");  
        jedis.zadd("hackers", 1953, "Richard Stallman");  
        jedis.zadd("hackers", 1965, "Yukihiro Matsumoto");  
        jedis.zadd("hackers", 1916, "Claude Shannon");  
        jedis.zadd("hackers", 1969, "Linus Torvalds");  
        jedis.zadd("hackers", 1912, "Alan Turing");  
        jedis.zadd("hackers", 1912, "Alan Turing111");  
        jedis.zadd("hackers", 2012, "Alan Turing2222");  
        Set<String> setValues = jedis.zrange("hackers", 0, -1);  //ASC从小到大
        System.out.println(setValues);  
        Set<String> setValues2 = jedis.zrevrange("hackers", 0, -1);  //DESC 从大到小
        System.out.println(setValues2);  

	}

	  protected static byte[] serialize(Object object)
	  {
	    ObjectOutputStream oos = null;
	    ByteArrayOutputStream baos = null;
	    try {
	      baos = new ByteArrayOutputStream();
	      oos = new ObjectOutputStream(baos);
	      oos.writeObject(object);
	      return baos.toByteArray();
	    }
	    catch (Exception e) {
	      e.printStackTrace();
	    }
	    return null;
	  }
	  
	  
}
